package Controller;

import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Model.CustomerModel;
import POJO.UserPOJO;

/**
 * Servlet implementation class customer
 */
@WebServlet("/customer")
public class Customer extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Customer() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		UserPOJO user  =  (UserPOJO)session.getAttribute("user");

		if(user != null)
		{
			//kiem tra phan quyen
			if(user.getType().equals("Customer Service") || user.getType().equals("System Admin")  )
			{
				request.setAttribute("do", "true");
			}
			
			request.setAttribute("menu", session.getAttribute("menu"));
			request.setAttribute("user", user);
			request.setAttribute("view", "Customers");
			request.setAttribute("url", "customer");
			if(request.getParameter("add")!= null)
			{
				request.setAttribute("action", "Add");
				RequestDispatcher rd = request.getRequestDispatcher("View/Add.jsp");
				rd.forward(request, response);
			}
			else if(request.getParameter("id")!= null)
			{
				request.setAttribute("action", "Add");
				request.setAttribute("id",request.getParameter("id"));
				RequestDispatcher rd = request.getRequestDispatcher("View/Add.jsp");
				rd.forward(request, response);
			}
			else
			{
				request.setAttribute("action", "View");
				RequestDispatcher rd = request.getRequestDispatcher("View/View.jsp");
				rd.forward(request, response);
			}
		}
		else
		{
			response.sendRedirect("login");
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String choose=request.getParameter("do");
		switch(choose)
		{
		case"Add":{InsertCustomer(request,response); break;}
		case"delete":{DeleteCustomer(request,response);break;}
		case"Edit":{UpdateCustomer(request,response);break;}
		case "Search":{response.sendRedirect("customer?key=" + request.getParameter("key"));break;}
		}
	}
	private void DeleteCustomer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException  
	{
		String id=request.getParameter("id");
		int kq=CustomerModel.DeleteCustomer(id);
		HttpSession session = request.getSession();
		if(kq==1)
		{
			session.setAttribute("doSuscess", "Customer deleted sucessfully");
		}
		else
		{
			session.setAttribute("doSuscess", "Customer deleted sucessfully");
			
		}
		response.sendRedirect("customer");
	}
	private void InsertCustomer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException //Insert
	{
		String name=request.getParameter("txtname");
		String dd=request.getParameter("d");
		String mm=request.getParameter("m");
		String yy=request.getParameter("y");
		String birth=mm+"/"+dd+"/"+yy;
		String address=request.getParameter("txtAddress");
		String tel=request.getParameter("txtTelephone");
		String sex=request.getParameter("sex");
		HttpSession session = request.getSession();
		if(name.equals("")|| birth.equals("")||tel.equals("")||address.equals(""))
		{
			session.setAttribute("doError", "You must insert full information");
		}
		else
		{
			int kq=CustomerModel.AddCustomer(name, birth, address, tel, sex);
			if(kq==1)
			{
				session.setAttribute("doSuscess", "Customer inserted sucessfully");
			}
			else
			{
				session.setAttribute("doSuscess", "Customer inserted fail");
			}
		}
		response.sendRedirect("customer");
	}
	private void UpdateCustomer(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException //Insert
	{
		String name=request.getParameter("txtname");
		String dd=request.getParameter("d");
		String mm=request.getParameter("m");
		String yy=request.getParameter("y");
		String birth=mm+"/"+dd+"/"+yy;
		String address=request.getParameter("txtAddress");
		String tel=request.getParameter("txtTelephone");
		String sex=request.getParameter("sex");
		String id=request.getParameter("idedit");
		HttpSession session = request.getSession();
		if(name.equals("")|| birth.equals("")||tel.equals("")||address.equals(""))
		{
			session.setAttribute("doError", "You must insert full information");
			
		}
		else
		{
			int kq=CustomerModel.UpdateCustomer(name, birth, address, id, tel, sex);
			if(kq==1)
			{
				session.setAttribute("doSuscess", "Customer updated sucessfully");
			}
			else
			{
				session.setAttribute("doSuscess", "Customer updated failed");
			}
		}
		response.sendRedirect("customer");
	}

}
